<template>
  <div >

    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
     <!-- <el-form-item label="姓名" prop="memberId">
        <el-input v-model="form.memberId" placeholder="请输入会员"/>
      </el-form-item>-->
      <el-form-item label="会员卡号" prop="cardNumber">
        <el-input v-model="form.cardNumber" placeholder="请输入会员卡号"/>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名"/>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入手机号"/>
      </el-form-item>



      <!--   <el-form-item label="会员卡号" prop="cardNumber">
           <el-input v-model="form.cardNumber" placeholder="请输入会员卡号"/>
         </el-form-item>-->
      <el-form-item label="生日" prop="birthday">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="form.birthday"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="车程" prop="drivingDistance">
        <el-input v-model="form.drivingDistance" placeholder="请输入车程"/>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"/>
      </el-form-item>

      <el-form-item label="图片" prop="img2">
        <el-upload
          class="avatar-uploader"
          :action="uploadImgUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="form.img2" :src="imgpre +form.img2" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="其他图片" prop="img1">
        <el-upload
          class="avatar-uploader"
          :action="uploadImgUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess2"
          :before-upload="beforeAvatarUpload">
          <img v-if="form.img1" :src="imgpre +form.img1" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitOnlyForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<script>


  import {
    listMember,
    getMember,
    getzi,
    delMember,
    addMember,
    updateMember,
    exportMember,
    rollbackMember,
    getzi2
  } from "@/api/mahjong/member";
  import {handlerdata,handerImg} from "@/api/mahjong/utils";
  import defaultSettings from '@/settings'
  export default {
    name: "",

    data() {
      return {
        uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
        imgpre: defaultSettings.ossPreview,
        form: {

        }

      }
    },
    props:{
      form:{}
    },
    created() {

    },
    methods: {

      handleAvatarSuccess(res) {
        this.form.img2 = res.fileName
      },
      handleAvatarSuccess2(res) {
        this.form.img1 = res.fileName
      },

      beforeAvatarUpload(file) {


        const isJPG = handerImg(file.type);

        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      likesFormat(row, column) {
        return handlerdata(row.likes,this.likesOptions);
      },

      submitOnlyForm: function () {
        if (this.form.likes) {
          this.form.likes = this.form.likes.toString()
        }
        if (this.form.americanMahjong) {
          this.form.americanMahjong = this.form.americanMahjong.toString()
        }
        if (this.form.custom) {
          this.form.custom = this.form.custom.toString()
        }
        if (this.form.customMahjong) {
          this.form.customMahjong = this.form.customMahjong.toString()

        }
        if (this.form.different) {
          this.form.different = this.form.different.toString()
        }
        if (this.form.matchMahjong) {
          this.form.matchMahjong = this.form.matchMahjong.toString()
        }
        if (this.form.ordinaryMahjong) {
          this.form.ordinaryMahjong = this.form.ordinaryMahjong.toString()
        }
        if (this.form.playingTime) {
          this.form.playingTime = this.form.playingTime.toString()
        }

        if (this.form.taboo) {
          this.form.taboo = this.form.taboo.toString()
        }
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateMember(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  // this.open = false;
                  // this.open2 = false;
                  this.$emit('getData');
                  this.$emit('update:close', false)

                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addMember(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.open2 = false;
                  this.$emit('getData');
                  this.$emit('update:close', false)
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      cancel() {
        this.$emit('update:close', false)
      },
      // submitOnlyForm() {
      //     this.$emit('update:close', false)
      //     this.$message({
      //         message: '会员创建成功',
      //         type: 'success'
      //     });
      // },

    }
  };
</script>

<style scoped rel="stylesheet/scss" lang="scss">
  .dialog-footer {
    text-align: right;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
